﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LiveWebServiceBase;
using LiveWebServiceBase.ServiceUserSettings;

namespace LiveWebServiceAdmin.ActionPages
{
    public partial class manageorganizations : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            string manageuser = string.Empty;

            if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) &&
                !string.IsNullOrEmpty(Page.Request.QueryString["editorganization"])/* &&
                Session["IsProcessed"] == null*/)
            {
                //if (Config.IsServiceUser(this.User.Identity.Name) && this.User.Identity.Name == Page.Request.QueryString["manageuser"])
                //    manageuser = this.User.Identity.Name;
                //else if (Config.IsAdminUser(this.User.Identity.Name))
                //    manageuser = Page.Request.QueryString["manageuser"];

                //if (!string.IsNullOrEmpty(manageuser))
                {
                    if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                        manageuser = Page.Request.QueryString["manageuser"];
                    else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                        manageuser = this.User.Identity.Name;

                    UserSettings usItem = new UserSettings(manageuser);

                    Organization org = usItem.GetOrganization(Page.Request.QueryString["editorganization"]);

                    tbNewOrganizationName.Text = org.orgname;
                    tbNewSharedSecurityKey.Text = org.sharedsecretkey;
                    tbNewCampusName.Text = org.campus;
                    tbNewDepartmentName.Text = org.department;
                    tbNewDefaultGroupName.Text = org.defaultgroup;
                    tbNewELMSUrl.Text = org.elmsurl;

                    btnCreateNewOrganization.Text = "Save";

                    //Session["IsProcessed"] = true;
                }
            }

            //if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) &&
            //    !string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]) &&
            //    Session["IsProcessed"] == null)
            //{
            //    string editeduser = Page.Request.QueryString["manageuser"];

            //    RequiredFieldValidator2.Enabled = false;
            //    RequiredFieldValidator3.Enabled = false;

            //    Session["IsProcessed"] = true;
            //}
        }

        protected void btnCreateNewOrganization_Click(object sender, EventArgs e)
        {
            try
            {
                string manageuser = string.Empty;

                if (!string.IsNullOrEmpty(Page.Request.QueryString["manageuser"]))
                    manageuser = Page.Request.QueryString["manageuser"];
                else if (Settings.UserInfo.IsServiceUser(this.User.Identity.Name))
                    manageuser = this.User.Identity.Name;

                UserSettings usItem = new UserSettings(manageuser);

                Organization org = new Organization();
                org.sharedsecretkey = tbNewSharedSecurityKey.Text;
                org.orgname = tbNewOrganizationName.Text;
                org.campus = tbNewCampusName.Text;
                org.department = tbNewDepartmentName.Text;
                org.defaultgroup = tbNewDefaultGroupName.Text;
                org.elmsurl = tbNewELMSUrl.Text;

                if (string.IsNullOrEmpty(Page.Request.QueryString["editorganization"]))
                    usItem.CreateOrganization(org);
                else
                    usItem.EditOrganization(Page.Request.QueryString["editorganization"], org);

                /*Session["IsProcessed"] = null;*/
                IsCompleted.Value = "true";
            }
            catch (Exception ex)
            {
                lblErrorTextNewOrganization.Text = ex.Message;
            }
        }
    }
}
